<template>
	<view>
		<view class="container">
			<view v-if="type == 0"><u-icon name="close-circle-fill" color="#e45656" size="100rpx" ></u-icon></view>
			<view v-if="type == 1"><u-icon name="checkmark-circle-fill" color="#5ac725" size="100rpx" ></u-icon></view>
			<view v-if="type == 0"  style="color: #e45656; font-size: 40rpx;">支付失败</view>
			<view v-if="type == 1"  style="color: #5ac725; font-size: 40rpx;">支付成功</view>
			<view class="txt">订单编号 {{orderId}}</view>
			
			<view v-if="type == 0" class="btnbox">
				<u-button text="重新支付" type="primary" size="large" shape="circle" customStyle="width: 230rpx" @click="handlePayment"></u-button>
				<u-button text="放弃订单" type="primary" size="large" shape="circle" plain customStyle="width: 230rpx" @click="navback"></u-button>
			</view>
			<view v-if="type == 1" class="btnbox">
				<u-button text="返回首页" type="primary" size="large" shape="circle" customStyle="width: 450rpx" @click="navtouser"></u-button>
			</view>
		</view>
		<u-overlay :show="showuploadImage">
			<view class="autloading"
				style="width: 100%;height: 100%;display: flex;justify-content: center;align-items: center;">
				<u-loading-icon :show="showuploadImage" color="white"></u-loading-icon>
			</view>
		
		</u-overlay>
	</view>
</template>

<script>
	import Index from "@/common/index-model.js"
	let index = new Index();
	let that = null;
	export default {
		data() {
			return {
				type:'',
				sends:'',
				orderId:'',
				showuploadImage:false
			}
		},
		onLoad(opt) {
			this.type = opt.type
			if(opt.type == 0){
				this.sends = JSON.parse(opt.sends)
				this.orderId = opt.orderId
				this.sends.orderId = opt.orderId
			}
			console.log(this.type,this.sends)
		},
		methods: {
			handlePayment(){
				this.showuploadImage =true
				
							index.getPublicUserMember(this.sends, '/wxPay/miniAppPay', 'post', res => {
								if (res.data.code == 200 && res.data.success) {
									this.showuploadImage =false
									console.log(res)
									uni.requestPayment({
										provider: 'wxpay',
										timeStamp: JSON.parse(res.data.result.orderInfo).timeStamp, // 时间戳
										nonceStr: JSON.parse(res.data.result.orderInfo).nonceStr, // 随机字符串
										package: JSON.parse(res.data.result.orderInfo).package, // 统一下单接口返回的prepay_id参数值
										signType: JSON.parse(res.data.result.orderInfo).signType, // 签名算法
										paySign: JSON.parse(res.data.result.orderInfo).paySign, // 签名
										success: (payRes) => {
											console.log('支付成功', payRes)
											uni.redirectTo({
												url: "/pagesB/rechargeVip/chargeResult?type=1&&orderId=" + res.data.result.orderId
											});
										},
										fail: (err) => {
											console.error('支付失败', err)
											uni.redirectTo({
												url: "/pagesB/rechargeVip/chargeResult?type=0&&orderId=" + res.data.result.orderId + "&&sends=" + JSON.stringify(this.sends)
											});
										}
									});
								} else {
									this.showuploadImage =false
									index.show_tips(res.data.message)
									return false
								}
							})	
			},
			navback(){
				index.getPublicUserMember({},'/apclient/appOrderInfo/cancelOrder/'+this.orderId,'post', res => {
					if (res.data.code == 200 && res.data.success) {
						console.log('关闭订单',res)
						index.navigate_back()
					}else{
						index.show_tips(res.data.message)
						return false
					}
				})
				
			},
			navtouser(){
				index.switch_tab('/pages/user/index')
			}
		}
	}
</script>

<style lang="scss">
	.container{
		display: flex;
		flex-direction: column;
		align-items: center;
		margin-top: 40rpx;
	}
	.txt{
		color: #666666;
		font-size: 30rpx;
	}
	.btnbox{
		width: 80%;
		margin: 0 auto;
		display: flex;
		justify-content: space-between;
		padding: 50rpx 30rpx;
	}
</style>